/*
 * Copyright (c) 2013, FPX and/or its affiliates. All rights reserved.
 * Use, Copy is subject to authorized license.
 */
package pyip.lib.javalearn.thread.exit;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

/**
 * @author yepeng
 * @date 2015年4月23日
 */
public class MainSubLifeCycleDemoTest {
    private static int executeCount;

    @Before
    public void before() {
        System.out.println();
        System.out.println(executeCount++ + " -------------------------------------------");
    }

    @After
    public void after() {
        System.out.println("main exit");
    }

    /**
     * 正常情况下, 父线程结束(包括Main), 子线程可以继续执行;<br>
     * <br>
     * 但在junit中, Main结束后, 子线程也会结束; 非Main的父线程结束后, 子线程可以继续执行.<br>
     * 为什么会这样, 原来junit退出时调用了 System.exit(0);<br>
     * <br>
     * <b>另外</b><br>
     * 线程分为守护线程和非守护线程，通过isDaemon()可以查看。<br>
     * Java进程在所有的非守护线程结束后，会全部结束，所有守护线程都会停止。如果只是某个父线程结束，它产生的子线程不会受到影响。<br>
     * 一个线程是否是守护线程默认和他的父线程及ThreadGroup有关，但可以在start以前通过setDaemon()自行设置。 <br>
     * <br>
     * 守护线程需要通过interrupt()或者系统所有非守护线程都退出才会退出, 并不是只看父线程, 还有setDaemon()只有在start之前设置才有用。
     * @see #testParentEnd()
     */
    @Test
    public void testMainEnd() {
        System.out.println("#testMainEnd");
        System.out.println("isDaemon: " + Thread.currentThread().isDaemon());

        new Thread() {
            public void run() {
                System.out.println("\tisDaemon: " + Thread.currentThread().isDaemon());

                new Thread() {
                    public void run() {
                        System.out.println("\t\tisDaemon: " + Thread.currentThread().isDaemon());
                        try {
                            Thread.sleep(2000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        System.out.println("\t\tsub thread exit");
                    }
                }.start();

                System.out.println("\tparent thread exit");
            }
        }.start();
    }

    /**
     * @see #testMainEnd()
     */
    @Test
    public void testParentEnd() {
        System.out.println("#testParentEnd");
        System.out.println("isDaemon: " + Thread.currentThread().isDaemon());

        new Thread() {
            public void run() {
                System.out.println("\tisDaemon: " + Thread.currentThread().isDaemon());

                new Thread() {
                    public void run() {
                        System.out.println("\t\tisDaemon: " + Thread.currentThread().isDaemon());
                        try {
                            Thread.sleep(2000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        System.out.println("\t\tsub thread exit");
                    }
                }.start();

                System.out.println("\tparent thread exit");
            }
        }.start();

        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
